// pages/order/order.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orders: null,
        show: false,
        // list: [],
        sum: null,
        order: null,
        plate: "",
        user: "",
        value: "", //省份
        phone: "",
        address: "",
        newaddress: "", //方便存储拼接好的地址
        way: "", //获取快递的方式
        beizhu: "", //备注的字段
        num: 0, //总价
        addresslist: [], // 存储过的地址数组
        state: null,
        index: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options)
        let carts = wx.getStorageSync('bookcarts')
        let arr = carts.filter(item => {
            return item.checked == true
        })

        // 商品合计
        let sum = arr.reduce((num, item) => {
            return num + item.price * item.count
        }, 0)
        this.setData({
            orders: arr,
            sum
        })
        for (var i = 0; i < carts.length; i++) {
            carts[i].checked = false
        }
        wx.setStorageSync("bookcarts", carts)
            // 从购物车跳转过来的
        if (options.orders) {
            console.log(options.orders)
            let orders = JSON.parse(options.orders)
            let newaddress = wx.getStorageSync('newaddress')
            wx.setStorageSync('orders', orders)
                // 本地是否存储地址
            this.setData({
                orders,
                address: newaddress ? newaddress : "",
                newaddress
            })
            let addressArr = wx.getStorageSync('addresslist');
            this.setData({
                addresslist: addressArr
            })
            this.getnum()
        } else if (options.user) {

            console.log("options", options)
                // 从地址信息跳转过来的

            let {
                user,
                value,
                phone,
                address,
                plate,
                index,
                state,
            } = options
            // console.log(state)
            // 从地址栏中返回，没有携带商品信息

            let orders = wx.getStorageSync('orders')
                // let newaddress = wx.getStorageSync('newaddress')
            this.setData({
                orders,
                // newaddress
            })
            let newaddress = address + ' ' + user + ' ' + phone + ' ' + value + ' ' + plate
                // console.log(newaddress)
            wx.setStorageSync('newaddress', newaddress)
            this.setData({
                user,
                phone,
                address,
                orders,
                newaddress,
                plate,
                state,
            })
            let addressArr = wx.getStorageSync('addresslist') || [];
            // console.log(addressArr)
            // console.log(index)
            if (addressArr) {
                if (index >= 0) {
                    console.log(this.data.state)
                    if (this.data.state == 'delete') {
                        console.log('删除了', index, addressArr)
                            // addressArr.splice(index, 1)
                        console.log(addressArr)
                        this.setData({
                            newaddress: addressArr[0]
                        })
                        wx.setStorageSync('newaddress', this.data.newaddress)
                    } else if (this.data.state == 'edit') {
                        addressArr.splice(index, 1, this.data.newaddress)
                    }
                } else {
                    let boolitem = addressArr.find((item) => {
                        return item == newaddress
                    })
                    if (!boolitem) {
                        addressArr.push(newaddress)
                    }

                    // console.log("222")

                }
                this.setData({
                    addresslist: addressArr
                })
                wx.setStorageSync('addresslist', this.data.addresslist)
                this.getnum()
            }
        } else if (options.title) {
            // 立即购买跳转过来的
            let sum = options.price * options.count
            let arr = []
            arr.push(options)
            console.log(arr)
            this.setData({
                orders: arr,
                sum
            })
            wx.setStorageSync('orders', arr)
        }
    },
    // 开启弹窗
    showPopup() {
        this.setData({
            show: true
        });
    },
    // 关闭弹窗
    onClose() {
        this.setData({
            show: false
        });
    },
    // 弹窗新增地址
    addressfn() {
        this.setData({
            state: "add"
        })
        wx.navigateTo({
            url: '/pages/inmar/inmar'
        })
    },
    // 合计
    getnum() {
        let nums = this.data.orders.reduce((num, item) => {
            return num + item.price * item.count
        }, 0)
        this.setData({
            sum: nums
        })
    },
    // 修改地址
    addressedit(e) {
        this.setData({
            state: "edit"
        })
        console.log("222", e.currentTarget.dataset.item)
        let index = e.currentTarget.dataset.id
        console.log("index", index)
        let a = e.currentTarget.dataset.item.split(" ")
        this.setData({
            value: a[3] + ' ' + a[4] + ' ' + a[5],
            user: a[1],
            phone: a[2],
            address: a[0],
            plate: a[6],
        })
        wx.navigateTo({
            url: '../inmar/inmar?user=' + this.data.user + '&phone=' + this.data.phone + '&value=' + this.data.value + '&address=' + this.data.address + '&plate=' + this.data.plate + '&index=' + index + '&state=' + this.data.state
        })
    },
    // 提价订单
    onSubmit(res) {
        console.log(res.currentTarget.dataset.submits)
        let {
            title,
            total
        } = JSON.parse(res.currentTarget.dataset.submits)
        wx.yunRequestPayment({
            timeStamp: '111',
            nonceStr: '222',
            _package: 'prepay_id=wx11111',
            signType: 'MD5',
            paySign: title + '##' + total + '##233##233##234##256899',
            success(res) {
                console.log(res)
            },
            fail(res) {
                console.log(res)
            }
        })
    },
    onshow: function() {

    }

})